import axios from 'axios'
import cache from '@/util/cache.js'
class XCRequest {
  static instance
  constructor(config) {
    this.instance = axios.create(config)
    this.instance.interceptors.request.use(
      (config) => {
        //添加头信息
        config.headers['content-type'] = 'application/x-www-form-urlencoded'
        //添加token
        config.headers.Authorization = `Bearer ${cache.getCache('jxcms-token')}`
        //添加加载动画
        return config
      },
      (err) => {
        return err
      }
    )

    this.instance.interceptors.response.use(
      (res) => {
        // 拦截响应的数据
        return res.data
      },
      (err) => {
        return err
      }
    )
  }
  request(config) {
    return new Promise((resolve, reject) => {
      this.instance
        .request(config)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
  get(config) {
    return this.request({ ...config, method: 'get' })
  }
  post(config) {
    return this.request({ ...config, method: 'post' })
  }
}
export default XCRequest
